/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.integralorders.service;

import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.pointslottery.entity.PointsLottery;
import com.jeeplus.modules.pointslottery.service.PointsLotteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.integralorders.entity.IntegralOrders;
import com.jeeplus.modules.integralorders.mapper.IntegralOrdersMapper;

/**
 * 积分订单Service
 * @author w
 * @version 2021-03-11
 */
@Service
@Transactional(readOnly = true)
public class IntegralOrdersService extends CrudService<IntegralOrdersMapper, IntegralOrders> {

	@Autowired
	private AddressService addressService;
	@Autowired
	private PointsLotteryService pointsLotteryService;
	@Autowired
	private AccountService accountService;

	public IntegralOrders get(String id) {
		return super.get(id);
	}
	
	public List<IntegralOrders> findList(IntegralOrders integralOrders) {
		return super.findList(integralOrders);
	}
	
	public Page<IntegralOrders> findPage(Page<IntegralOrders> page, IntegralOrders integralOrders) {
		return super.findPage(page, integralOrders);
	}
	
	@Transactional(readOnly = false)
	public void save(IntegralOrders integralOrders) {
		super.save(integralOrders);
	}
	
	@Transactional(readOnly = false)
	public void delete(IntegralOrders integralOrders) {
		super.delete(integralOrders);
	}

	/**
	 * 积分购买
	 * @param pointsLottery
	 * @param member
	 * @param num
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson purchaseOrder(PointsLottery pointsLottery, Member member, int num) {
		AjaxJson aj = new AjaxJson();
		//购买的价格
		Double money = num * pointsLottery.getIntegralPrice();
		//查询用户收货地址
		Address address = new Address();
		address.setMember(member);
		List<Address> addressList = addressService.findList(address);
		Account account = accountService.get(member.getAccount().getId());
		if (account != null){
			if (!(money>account.getIntegral())){
				if (addressList.size()>0){
					//优先查询该用户是否购买过此商品并还没有开奖
					IntegralOrders integralOrders = new IntegralOrders();
					integralOrders.setMember(member);
					integralOrders.setPointLottery(pointsLottery);
					List<IntegralOrders> list = findList(integralOrders);
					if (list.size()>0){
						//买过
						list.get(0).setBuyAmount(list.get(0).getBuyAmount() + num);
						list.get(0).setMoney(list.get(0).getMoney() + money);
						save(list.get(0));
					}else {
						//第一次购买
						SnowFlake snowFlake = new SnowFlake(2,3);
						Long orderNumber = snowFlake.nextId();
						IntegralOrders entity = new IntegralOrders();
						entity.setOrderNo(orderNumber.toString());
						entity.setMoney(money);
						entity.setBuyAmount(num);
						entity.setPointLottery(pointsLottery);
						entity.setMember(member);
						entity.setAddress(addressList.get(0).getAddress());
						entity.setIsEnd("0");
						entity.setPrizeFlag("0");
						save(entity);
					}
					if (pointsLottery.getIntegralMember()-num == 0){
						pointsLottery.setIsEnd("1");
					}
					pointsLottery.setIntegralMember(pointsLottery.getIntegralMember()-num);
					pointsLotteryService.save(pointsLottery);
					//如果就一个人所有全买了，中奖必定是他
					IntegralOrders o = new IntegralOrders();
					o.setPointLottery(pointsLottery);
					List<IntegralOrders> list1 = findList(o);
					if (list1.size() == 1 && pointsLottery.getIntegralMember() == 0){
						for (IntegralOrders orders : list1) {
							orders.setPrizeFlag("1");
							orders.setIsEnd("1");
							save(orders);
						}
					}
					account.setIntegral(account.getIntegral()-money);
					accountService.save(account);

//			//记录
//			DynamicRecord dynamicRecord = new DynamicRecord();
//			dynamicRecord.setMember(member);
//			dynamicRecord.setFrommem(member);
//			dynamicRecord.setName("积分兑换");
//			dynamicRecord.setMoney(payIntegral);
//			dynamicRecordService.save(dynamicRecord);
				}
			}else {
				aj.setMsg("积分不足");
				aj.setSuccess(false);
			}

		}else {
			aj.setMsg("账户信息有误");
			aj.setSuccess(false);
		}

		return aj;
	}

	@Transactional(readOnly = false)
	public void makePrice(IntegralOrders integralOrders) {
		integralOrders.setIsEnd("1");
		save(integralOrders);
	}
}